home *** CD-ROM | disk | FTP | other *** search
- Path: news1.h1.usa.pipeline.com!usenet
- From: grantp@usa.pipeline.com(Pete)
- Newsgroups: comp.lang.c++
- Subject: Re: Two way communication between objects
- Date: 28 Jan 1996 01:51:48 GMT
- Organization: Kalevi, Inc
- Message-ID: <4eekrk$cu2@news1.usa.pipeline.com>
- NNTP-Posting-Host: pipe7.h1.usa.pipeline.com
- X-PipeUser: grantp
- X-PipeHub: usa.pipeline.com
- X-PipeGCOS: (Pete)
- X-Newsreader: Pipeline USA v3.3.0
-
- On Jan 28, 1996 11:09:54 in article <Two way communication between
- objects>, 'Ross Forder <erosco@werple.mira.net.au>' wrote:
-
- >I am trying to write a VERY simple client and server set of objects. I
- >would like to client to register with the server at ctor time and have
- >the server know about the client by saving a pointer to the client
- >so he can callback to a method called say 'event'.
- >
- >The problem is the fact that they will both need a pointer to each other
- >and this seems impossible using strong typing. Is there a simple 'object
- >oriented' way to do this?
- >
-
- OO notwithstanding, a "pointer" to an object in another process, be it
- on your home machine or across the net, is invalid -- at least as an
- actual pointer. Of course, you can pass a pointer to an object between
- processes as a "handle", but the pointer is worthless to the other side.
-
- But your question is quite on a different subject; i.e., can two objects
- have pointers to each other? Sure can. Example:
-
- class A; // forward declaration
-
- class B { public: B(A* a) : a(a) {} private: A * a; };
-
- class A { public: A() : b(0) {} void SetB(B* obj) {b = obj}; private: B* b;
- };
-
- int main ()
- {
- A a;
- B b(&a);
- a.SetB(&b);
- // Now, a has a pointer to b and b has a pointer to a
- return 0;
- }
-
- >I have been able to make this work by having the server only know about
- >a parent class of the client (say eventhandler) but this is still not a
- >bulletproof solution.
- >
- A bit of semantics here. I believe that you mean knowing about the
- parent instance rather than about the class. Knowing about the parent's
- class is meaningless as you can't call any of its members directly through
- the pointer.
-
- The farther I get into trying to answer your question, the more
- convinced I'm becoming that I don't really understand what you are
- trying to do. In a client-server environment, there is a connection
- between the client and the server and information is passed
- back and forth between the two in some predefined format which
- both are able to handle. Often this is is handled for the programmer
- through a mechanism such as RPC. RPC makes the interprocess
- calls look just like local function calls. For example, if you have
- a function in the server whose prototype is
- char * GetTheFoolsName(int id);
- then you can just code char * name = GetTheFoolsName(5) and
- RPC takes care of transporting a message to the server to
- invoke its function and returning its result back to you. But you
- can't call member functions this way.
-
- >Can someone set me straight?
- >
- I don't think I did. I really should trash this response, but what
- the heck, it's Saturday and I'm tired. I'll post it anyway.
-
- --
-
- Pete
-
-
-
-
-
-